package com.rocky.component.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Objects;

/**
 * @author rocky
 * date 2025-08-16
 * Description:
 */
public class ObjectReflectUtil {

    public static String getFieldValue(Object object, Class<? extends Annotation> annotationClass) {
        for (Field field : object.getClass().getDeclaredFields()) {
            if (field.isAnnotationPresent(annotationClass)) {
                Object fieldValue = getFieldValue(field, object);
                if (Objects.isNull(fieldValue)) {
                    return null;
                }

                return fieldValue.toString();
            }
        }

        return null;
    }

    public static Object getFieldValue(Field field, Object object) {
        field.setAccessible(true);

        try {
            return field.get(object);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
